<?php
	
	function getPlayerInfo($line)
	{
		//find dot after player rank and replace with pipe delimiter
		$rankToPlayerDotPos = strpos($line, ". ");
		$line = substr_replace($line, "|", $rankToPlayerDotPos, 2);

		//find comma after player name and replace with pipe delimiter
		$playerToTeamCommaPos = strpos($line, ", ");
		$line = substr_replace($line, "|", $playerToTeamCommaPos, 2);

		//find space after player team and replace with pipe delimiter
		$teamToPositionSpacePos = strpos($line, " ", $playerToTeamCommaPos);
		$line = substr_replace($line, "|", $teamToPositionSpacePos, 1);

		//remove space after comma between player positions
		$playerPositionDelimiterPos = strpos($line, " ", $teamToPositionSpacePos);
		if ($playerPositionDelimiterPos !== false)
			$line = substr_replace($line, "", $playerPositionDelimiterPos, 1);


		$line = str_replace("|YEAR|GP|MIN|FG%|FT%|3PM|REB|AST|A/TO|STL|BLK|TO|PTS",
				"", $line);
		return $line;
	}

	function getPlayerStats($line)
	{
		//remove stat year column
		$line = substr($line, strpos($line, "|"));

		//remove assists to turnover ratio column
		$length = strlen($line);
		$start = $length;
		for ($i = 0; $i < 5; $i++)
		{
			$offset = ($length + 1 - $start) * -1;
			$start = strrpos($line, '|', $offset);
		}
		$atoLength = strpos($line, "|", $start + 1) - $start;

		return substr_replace($line, "", $start, $atoLength);
	}

	function capitalizeTeam($playerData)
	{
		$start = strpos($playerData, "|");
		$start = strpos($playerData, "|", $start + 1) + 1;
		$length = strpos($playerData, "|", $start) - $start;

		$team = strtoupper(substr($playerData, $start, $length));
		return substr_replace($playerData, $team, $start, $length);
	}

	function trimExtraSpaces($playerData)
	{
		//get position of second pipe delimiter
		$firstPipePos = strpos($playerData, "|");
		$secondPipePos = strpos($playerData, "|", $firstPipePos + 1);

		//divide the string into two using the position of the delimiter
		$firstSection = substr($playerData, 0, $secondPipePos);
		$secondSection = substr($playerData, $secondPipePos);

		//trim second section of the divided string
		$secondSection = preg_replace('/\s+/', '', $secondSection);

		return $firstSection . $secondSection;
	}


	$filename = "espnrankings.data";
	$file = file_get_contents("raw/" . $filename);
	$lines = explode("\n", $file);
	$playerList = "RANK|NAME|TEAM|POS|GP|MIN|FG%|FT%|3PM|REB|AST|A/TO|STL|BLK|TO|PTS|pGP|pMIN|pFG%|pFT%|p3PM|pREB|pAST|pA/TO|pSTL|pBLK|pTO|pPTS";
	foreach($lines as $line)
	{
		$lineStart = substr($line, 0, 4);
		
		if ($lineStart == "****")
		{
			$playerData = trimExtraSpaces($playerData);
			$playerList .= "\n" . $playerData;
		}
		else if ($lineStart == "2016" || $lineStart == "2015")
		{
			$playerData .= getPlayerStats($line);
		}
		else
		{
			$playerData = getPlayerInfo($line);
			$playerData = capitalizeTeam($playerData);
		}
	}

	if (file_put_contents("out/" . $filename, $playerList) !== false)
	{
		echo "<h3>Successfully processed raw/$filename.</h3>" .
			"<h3>Processed data is at out/$filename.</h3>";
		echo "<iframe src='out/$filename' width='100%' height='80%'>";
	}
	else
	{
		echo "<h3>Error processing raw data.</h3>";
	}
?>